local qicai = fk.CreateSkill {

  name = "joy_mou__qicai",

  tags = {  },

}



qicai:addEffect("active", {
  name = "joy_mou__qicai",
  anim_type = "switch",
  switch_skill_name = "joy_mou__qicai",
  card_num = function ()
    return player:getSwitchSkillState("joy_mou__qicai", false) ~= fk.SwitchYang and 1 or 0
  end,
  target_num = 0,
  prompt = function ()
    return player:getSwitchSkillState("joy_mou__qicai", false) == fk.SwitchYang and "令你使用的下一张普通锦囊牌可增加或减少一个目标" or "你可以弃置一张基本牌，获得一张锦囊牌"
  end,
  can_use = function ()
    if player:getSwitchSkillState("joy_mou__qicai", false) == fk.SwitchYang then
      return player:getMark("joy_mou__qicai1-turn") < 2
    else
      return player:getMark("joy_mou__qicai2-turn") < 2
    end
  end,
  card_filter = function(self, player, to_select, selected)
    if player:getSwitchSkillState("joy_mou__qicai", false) == fk.SwitchYang then
      return false
    else
      return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeBasic
    end
  end,
  target_filter = function(self, player, to_select, selected)
    return false
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local switch = player:getSwitchSkillState(qicai.name, true,false)
    if switch == fk.SwitchYang then
      room:addPlayerMark(player,"joy_mou__qicai1-turn",1)
      room:setPlayerMark(player,"@@joy_mou__qicai",1)
    else
      room:addPlayerMark(player,"joy_mou__qicai2-turn",1)
      room:throwCard(effect.cards,qicai.name,player,player)
      if not player.dead then
        local cards = room:getCardsFromPileByRule(".|.|.|.|.|trick",1,"allPiles")
        if #cards > 0 then
        room:moveCards({
        ids = cards,
        to = player.id,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        proposer = player.id,
        skillName = qicai.name,
        })
        end
      end
    end
  end,
})

qicai:addEffect(fk.GameStart, {
  name = "#joy_mou__qicai_delay",
  events = {fk.GameStart},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(qicai.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = player.room:askForChoice(player,{"joy_mou__qicai1","joy_mou__qicai2"},qicai.name,"选择初始状态",true)
    if not choice then
      choice = table.random({"joy_mou__qicai1","joy_mou__qicai2"})
    end
    if choice == "joy_mou__qicai1" then
      room:notifySkillInvoked(player,qicai.name)
      --room:delay(200)
      player.room:setPlayerMark(player, MarkEnum.SwithSkillPreName .. qicai.name,fk.SwitchYang)
      player:setSkillUseHistory(qicai.name, 0, Player.HistoryGame)
    elseif choice == "joy_mou__qicai2" then
      room:notifySkillInvoked(player,qicai.name)
      --room:delay(200)
      player.room:setPlayerMark(player, MarkEnum.SwithSkillPreName .. qicai.name,fk.SwitchYin)
      player:setSkillUseHistory(qicai.name, 0, Player.HistoryGame)
    end
  end,
})
qicai:addEffect(fk.TargetConfirming, {
  refresh_events = {fk.TargetConfirming},
  can_refresh = function (self, event, target, player, data)
    return player.id == data.from and player:getMark("@@joy_mou__qicai") > 0 and data.card and data.card:isCommonTrick()
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player,"@@joy_mou__qicai",0)
    room:notifySkillInvoked(player, qicai.name,"offensive")
    player:broadcastSkillInvoke(qicai.name)
    local targets = data:getUseExtraTargets()
        local origin_targets = U.getActualUseTargets(room, data, event)
        if #origin_targets > 1 then
          table.insertTable(targets, origin_targets)
        end
        if #targets > 0 then
          local tos = player.room:askForChoosePlayers(player,targets, 1,1,
            "你可以为"..data.card:toLogString().."增加或减少一个目标",qicai.name, true)
          if #tos > 0 then
            local tos1,tos2 = {},{}
            for _, pid in ipairs(tos) do
              if table.contains(data:getAllTargets(),pid) then
                table.insertIfNeed(tos1,pid)
              else
                table.insertIfNeed(tos2,pid)
              end
            end
            if #tos1 > 0 then
              for _, pid in ipairs(tos1) do
                data:cancelTarget(pid)
              end
            end
            if #tos2 > 0 then
                AimGroup:addTargets(player.room, data,tos2)
            end
          end
        end
  end,
})

return qicai